import {message as antdMessage, Modal} from 'ant-design-vue'

// 消息工具函数
export const success = (content: string, duration = 3) =>
    antdMessage.success({content, duration})

export const error = (content: string, duration = 5) =>
    antdMessage.error({content, duration})

export const warning = (content: string, duration = 4) =>
    antdMessage.warning({content, duration})

export const info = (content: string, duration = 3) =>
    antdMessage.info({content, duration})

export const loading = (content: string, duration = 0) =>
    antdMessage.loading({content, duration})

// 错误处理
export const handleError = (err: any, fallbackMsg = '操作失败') => {
    const message = err?.response?.data?.message || err?.message || fallbackMsg
    error(message)
}

// 确认对话框
export const confirm = (config: {
    title: string
    content?: string
    onOk?: () => void | Promise<void>
}) => {
    return Modal.confirm({
        title: config.title,
        content: config.content,
        okText: '确定',
        cancelText: '取消',
        centered: true,
        onOk: config.onOk
    })
}

// 销毁所有消息
export const destroy = () => antdMessage.destroy()
